#include "log.h"
#include "single_tcpserver.h"
#include "iec10X_protocol.h"
#include <pthread.h>

static int RefVal(int* v)
{
    return *v;
}


int main()
{
    pthread_t proto_taskid;
    int exit_flag = 0;

    SingleTcpServer_T* tcp_srv = SingleTcpServer_Create();
    IEC10X_Protocol_T* proto104 = IEC10X_Protocol_Create();
    IEC10X_Protocol_SetIO(proto104, SingleTcpServer_Read, SingleTcpServer_Write, SingleTcpServer_Reset, tcp_srv);
    IEC10X_Protocol_SetInterrupter(proto104, RefVal, &exit_flag);

    pthread_create(&proto_taskid, NULL, IEC10X_Protocol_Run, proto104);
    
    getchar();
    exit_flag = 1;
    pthread_cancel(proto_taskid);
    pthread_join(proto_taskid, NULL);
    
    IEC10X_Protocol_Destroy(proto104);
    SingleTcpServer_Destroy(tcp_srv);

    return 0;
}
